<?php
// +----------------------------------------------------------------------
// | Author: fl 365581323@qq.com
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager; 
use app\index\model;
class Common extends Controller{
    // Request实例
    // protected $request;
    public function __construct(){
		parent::__construct(); // 调用父类的构造函数必须显示的使用parent调用父类构造函数
       
        header('content-type:application:json;charset=utf8');
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:POST,GET');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        require_once VENDOR_PATH.'qiniu/php-sdk/autoload.php';
        $uid=input('uid','');//获取用户的uid
        if(!empty($uid)){
          $nowTime=time();//用户的现在时间
           $user_info=Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>'1'])->field('token_time')->find();
           $token_time=$nowTime+1800;//过期30分钟
           if($nowTime<$user_info['token_time']){
              $data=['token_time'=>$token_time];
              $user_info=Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>'1'])->update($data);
           }
           
        }
       
	}
    
    //多维数组去重
    function assoc_unique(&$list, $key)
        {
            $arr=array();
            for($i=0;$i<count($list);$i++)
            {
                if(!isset($arr[$list[$i][$key]]))
                {
                    $arr[$list[$i][$key]]=$list[$i];
                }
            }
            $list=array_values($arr);
            return $list;
       } 
    //上传base64图片curl
   function request_by_curl($remote_server, $post_string, $token)
      {
       
          $headers = array();
          $headers[] = 'Content-Type:image/png';
          $headers[] = 'Authorization:UpToken ' . $token;
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $remote_server);
          //curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          //curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
          curl_setopt($ch, CURLOPT_TIMEOUT, 30);
          $data = curl_exec($ch);
          curl_close($ch);
       
          return $data;
      }
       function getToken(){
        $auth=new Auth();
        $bucket = "userhead";//上传空间名称
        return $auth->uploadToken($bucket);//生成token      
    }
    //上传七牛云
    function uploadImg($file){
       // 要上传图片的本地路径
            $filePath = $file->getRealPath();
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
            // 上传到七牛后保存的文件名
            $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
            //空间域名
            $domain = config('DOMAINTOW');
            $token=$this->getToken();
            // $token = $auth->uploadToken($bucket);
            // 初始化 UploadManager 对象并进行文件的上传
            $uploadMgr = new UploadManager();
            // 调用 UploadManager 的 putFile 方法进行文件的上传
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            if ($err !== null) {
                return 0;  
            } else {
              return $domain.$ret['key'];
                
            }
    }
    /**
     * 处理数组null值
     * @param array $arr
     * @return array
     */
    function array_nonull($arr)
    {
        if(is_array($arr)){
            foreach ($arr as $k=>$v) {
                if(is_array($v)) {
                    $arr[$k] = $this->array_nonull($v);
                }else {
                    if($v===null) {
                        $arr[$k]='';
                    }else{
                        $arr[$k]=strval($v);
                    }
                }
            }
            return $arr;
        }else{
            if($arr===null){
                return '';
            }else{
                return strval($arr);
            }
        }
    }
    //对象转化数组
 function object2array($object) {
    if (is_object($object)) {
     foreach ($object as $key => $value) {
       $array[$key] = $value;
       }
     }
     else {
     $array = $object;
     }
     return $array;
    }
    /**
   * 请求短信接口返回内容
   * @param  string $url [请求的URL地址]
   * @param  string $params [请求的参数]
   * @param  int $ipost [是否采用POST形式]
   * @return  string
   */
  function juhecurl($url,$params=false,$ispost=0){
      $httpInfo = array();
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
      curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
      curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
      curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
      if( $ispost )
      {
          curl_setopt( $ch , CURLOPT_POST , true );
          curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
          curl_setopt( $ch , CURLOPT_URL , $url );
      }
      else
      {
          if($params){
              curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
          }else{
              curl_setopt( $ch , CURLOPT_URL , $url);
          }
      }
      $response = curl_exec( $ch );
      if ($response === FALSE) {
          //echo "cURL Error: " . curl_error($ch);
          return false;
      }
      $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
      $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
      curl_close( $ch );
      return $response;
  }
  /**
 * 计算两点地理坐标之间的距离
 * @param Decimal $longitude1 起点经度
 * @param Decimal $latitude1 起点纬度
 * @param Decimal $longitude2 终点经度 
 * @param Decimal $latitude2 终点纬度
 * @param Int   $unit    单位 1:米 2:公里
 * @param Int   $decimal  精度 保留小数位数
 * @return Decimal
 */
 public function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){
 
  $EARTH_RADIUS = 6370.996; //地球半径系数
  $PI = 3.1415926;
  $radLat1 = $latitude1 * $PI / 180.0;
  $radLat2 = $latitude2 * $PI / 180.0;
 
  $radLng1 = $longitude1 * $PI / 180.0;
  $radLng2 = $longitude2 * $PI /180.0;
 
  $a = $radLat1 - $radLat2;
  $b = $radLng1 - $radLng2;
 
  $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
  $distance = $distance * $EARTH_RADIUS * 1000;
 
  if($unit==2){
    $distance = $distance / 1000;
  }
 
  return round($distance, $decimal);
 
}
/**
 * 准备工作完毕 开始计算年龄函数
 * @param  $birthday 出生时间 uninx时间戳
 * @param  $time 当前时间
 **/
 function getAge($birthday){
    //格式化出生时间年月日
    $byear=date('Y',$birthday);
    $bmonth=date('m',$birthday);
    $bday=date('d',$birthday);

    //格式化当前时间年月日
    $tyear=date('Y');
    $tmonth=date('m');
    $tday=date('d');

    //开始计算年龄
    $age=$tyear-$byear;
    if($bmonth>$tmonth || $bmonth==$tmonth && $bday>$tday){
        $age--;
    }
    return $age;
}
/**
 * 网易云的header头部
 */
function setHeader(){
    $nonce='abcb123';
      $ctime=time();
      $csum=sha1('572faf2b1b76'.$nonce.$ctime);
      $headers = array();
      $headers[] = 'AppKey: 98b90dd621eef6bfc37b9a2917a96776';
      $headers[] = 'Nonce: '.$nonce;
      $headers[] = 'application/x-www-form-urlencoded;charset=utf-8;';
      $headers[] = 'CurTime: '.$ctime;
      $headers[] = 'CheckSum: '.$csum;
      return $headers;
  }

}